import { ArrowRightIcon } from "lucide-react";

import { commands as windowsCommands } from "@hypr/plugin-windows";
import { Button } from "@hypr/ui/components/ui/button";

import type { LLMConnectionStatus } from "../../../../../../hooks/useLLMConnection";

export function ConfigError({ status }: { status: LLMConnectionStatus }) {
  const handleConfigureClick = () => {
    windowsCommands
      .windowShow({ type: "settings" })
      .then(() => new Promise((resolve) => setTimeout(resolve, 1000)))
      .then(() =>
        windowsCommands.windowEmitNavigate(
          { type: "settings" },
          {
            path: "/app/settings",
            search: { tab: "intelligence" },
          },
        ),
      );
  };

  const message = getMessageForStatus(status);

  return (
    <div className="flex flex-col items-center justify-center h-full min-h-[400px]">
      <p className="text-sm text-center text-neutral-700 mb-6 max-w-lg">
        {message}
      </p>
      <Button
        onClick={handleConfigureClick}
        className="flex items-center gap-2"
        variant="default"
      >
        <span>Configure</span>
        <ArrowRightIcon size={16} />
      </Button>
    </div>
  );
}

function getMessageForStatus(status: LLMConnectionStatus): string {
  if (status.status === "pending" && status.reason === "missing_provider") {
    return "You need to configure a language model to summarize this meeting";
  }

  if (status.status === "pending" && status.reason === "missing_model") {
    return "You need to select a model to summarize this meeting";
  }

  if (status.status === "error" && status.reason === "unauthenticated") {
    return "You need to sign in to use Hyprnote's language model";
  }

  if (status.status === "error" && status.reason === "missing_config") {
    const missing = status.missing;
    if (missing.includes("api_key") && missing.includes("base_url")) {
      return "You need to configure the API key and base URL for your language model provider";
    }
    if (missing.includes("api_key")) {
      return "You need to configure the API key for your language model provider";
    }
    if (missing.includes("base_url")) {
      return "You need to configure the base URL for your language model provider";
    }
  }

  return "You need to configure a language model to summarize this meeting";
}
